#include "SwitchButton.h"
#include "models/command.h"
#include <QPainter>
#include <QPainterPath>
#include <QPropertyAnimation>

SwitchButton::SwitchButton(QWidget *parent) :
    QWidget(parent)
{
    resize(int(60*g_xScale), int(30*g_yScale)); // 默认60，30宽高
    m_bClicked = true;
    m_pSlider = new Slider(this);
    m_pSlider->resize(height() - m_nMargin * 2, height() - m_nMargin * 2);
    m_pSlider->move(m_nMargin, m_nMargin);
    m_bActive = false; // 默认未激活
    m_nArcRadius = std::min(width(), height()); // 默认半径
    m_nRectWidth = width() - m_nArcRadius;
    m_colorActive = Qt::green;
    m_colorInactive = Qt::gray;
    setCursor(QCursor(Qt::PointingHandCursor));
}

SwitchButton::~SwitchButton()
{
}

void SwitchButton::SetSize(int nWidth, int nHeight)
{
    resize(nWidth, nHeight);
    m_pSlider->resize(height() - m_nMargin * 2, height() - m_nMargin * 2);
    m_pSlider->move(m_nMargin, m_nMargin);
    m_nArcRadius = std::min(width(), height());
    m_nRectWidth = width() - m_nArcRadius;
}

void SwitchButton::SetActiveColor(const QColor& color)
{
    m_colorActive = color;
}

void SwitchButton::SetInactiveColor(const QColor& color)
{
    m_colorInactive = color;
}

void SwitchButton::SetSliderColor(const QColor& color)
{
    m_pSlider->SetSliderColor(color);
}

void SwitchButton::SetStatus(bool bActive)
{
    if(m_bActive == bActive || !m_bClicked)
    {
        return;
    }
    m_bClicked = false;
    m_bActive = bActive;
    if(m_bActive)
    {
        ToActive();
    }
    else
    {
        ToInactive();
    }
    emit Clicked(m_bActive);
}

bool SwitchButton::GetStatus() const
{
    return m_bActive;
}

void SwitchButton::paintEvent(QPaintEvent *)
{
    QPainter p;
    p.begin(this);
    p.setRenderHint(QPainter::Antialiasing, true);
    p.setPen(Qt::NoPen);
    if(m_bActive) p.setBrush(QBrush(m_colorActive));
    else p.setBrush(QBrush(m_colorInactive));

    QPainterPath leftPath;
    leftPath.addEllipse(0, 0, m_nArcRadius, m_nArcRadius);

    QPainterPath middlePath;
    middlePath.addRect(m_nArcRadius / 2, 0, m_nRectWidth, m_nArcRadius);

    QPainterPath rightPath;
    rightPath.addEllipse(m_nRectWidth, 0, m_nArcRadius, m_nArcRadius);

    QPainterPath path = leftPath + middlePath + rightPath;

    p.drawPath(path);

    p.end();
}

void SwitchButton::mousePressEvent(QMouseEvent *event)
{
    SetStatus(!m_bActive);
    QWidget::mousePressEvent(event);
}

void SwitchButton::ToActive()
{
    QPropertyAnimation* pAnimation = new QPropertyAnimation(m_pSlider, "geometry");
    pAnimation->setDuration(m_nDuration);
    pAnimation->setStartValue(m_pSlider->rect());
    pAnimation->setEndValue(QRect(width() - m_pSlider->width() - m_nMargin,
                                  m_nMargin,
                                  m_pSlider->width(),
                                  m_pSlider->height()));
    connect(pAnimation, &QPropertyAnimation::finished, this, [&]{
        if(!m_bClicked)
        {
            m_bClicked = true; // 结束后还原标识位
        }
    });
    connect(pAnimation, &QPropertyAnimation::valueChanged, this, [&](const QVariant &value){
        Q_UNUSED(value)
        update();
    });
    pAnimation->start(QAbstractAnimation::DeleteWhenStopped);
}

void SwitchButton::ToInactive()
{
    QPropertyAnimation* pAnimation = new QPropertyAnimation(m_pSlider, "geometry");
    pAnimation->setDuration(m_nDuration);
    pAnimation->setStartValue(QRect(m_pSlider->x(),
                                    m_pSlider->y(),
                                    m_pSlider->width(),
                                    m_pSlider->height()));
    pAnimation->setEndValue(QRect(m_nMargin,
                                  m_nMargin,
                                  m_pSlider->width(),
                                  m_pSlider->height()));
    connect(pAnimation, &QPropertyAnimation::finished, this, [&]{
        if(!m_bClicked)
        {
            m_bClicked = true; // 结束后还原标识位
        }
    });
    connect(pAnimation, &QPropertyAnimation::valueChanged, this, [&](const QVariant &value){
        Q_UNUSED(value)
        update();
    });
    pAnimation->start(QAbstractAnimation::DeleteWhenStopped);
}


///////////////////////////////////////////////////////////////////////////////////////////////
/// Slider  滑块类  //////////////////
//////////////////////////////////////////////////////////////////////////////////////////////

Slider::Slider(QWidget *parent) : QWidget(parent)
{
    m_sliderColor = Qt::white;
    resize(60, 60);
}

Slider::~Slider()
{

}

void Slider::SetSliderColor(const QColor &color)
{
    m_sliderColor = color;
    update();
}

void Slider::paintEvent(QPaintEvent *e)
{
    QPainter p(this);
    p.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
    p.fillRect(rect(), Qt::transparent);
    p.setBrush(m_sliderColor);
    p.setPen(Qt::NoPen);
    p.drawRoundedRect(rect(), width() / 2, height() / 2);
    QWidget::paintEvent(e);
}

